package cn.wjdiankong.levideo.list.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.view.dragsortview.DragSortAdapter;

public class DragSortTabGridAdapter extends DragSortAdapter {
	
	private List<Integer> dataList = new ArrayList<Integer>();
	@SuppressLint("UseSparseArrays")
	private Map<Integer, String> map = new HashMap<Integer, String>();
	private Context mContext;
	
    public DragSortTabGridAdapter(Context context, List<Integer> data) {
    	mContext = context;
        dataList = data;
        map.put(Const.VIDEO_TYPE_DOUYIN, "抖音");
        map.put(Const.VIDEO_TYPE_KUAISHOU, "快手");
        map.put(Const.VIDEO_TYPE_HUOSHAN, "火山");
        map.put(Const.VIDEO_TYPE_MIAOPAI, "秒拍");
    }

    @SuppressLint("InflateParams")
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.dialog_drag_sort_view_grid_item, null);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.item_img);
            holder.titleText = (TextView) convertView.findViewById(R.id.item_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if(getIconResId(dataList.get(position)) == -1){
        	holder.image.setVisibility(View.GONE);
        }else{
        	holder.image.setVisibility(View.VISIBLE);
        	holder.image.setImageResource(getIconResId(dataList.get(position)));
        }
        holder.titleText.setText(map.get(dataList.get(position)));
        return convertView;
    }

    public static class ViewHolder {
        public TextView titleText;
        public ImageView image;
    }

	@Override
	public void onReOrderContent(int fromPos, int toPos) {
		Integer value = dataList.remove(fromPos);
		dataList.add(toPos, value);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return dataList.size();
	}
	
	public List<Integer> getDataList(){
		return dataList;
	}
	
	private int getIconResId(int type){
		switch(type){
			case Const.VIDEO_TYPE_DOUYIN:
				return R.drawable.douyin;
			case Const.VIDEO_TYPE_HUOSHAN:
				return R.drawable.huoshan;
			case Const.VIDEO_TYPE_KUAISHOU:
				return R.drawable.kuaishou;
			case Const.VIDEO_TYPE_MIAOPAI:
				return R.drawable.miaopai;
		}
		return -1;
	}

}